<template>
  <el-dialog
    :title="!dataForm.attrId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="������" prop="attrName">
      <el-input v-model="dataForm.attrName" placeholder="������"></el-input>
    </el-form-item>
    <el-form-item label="�Ƿ���Ҫ����[0-����Ҫ��1-��Ҫ]" prop="searchType">
      <el-input v-model="dataForm.searchType" placeholder="�Ƿ���Ҫ����[0-����Ҫ��1-��Ҫ]"></el-input>
    </el-form-item>
    <el-form-item label="����ͼ��" prop="icon">
      <el-input v-model="dataForm.icon" placeholder="����ͼ��"></el-input>
    </el-form-item>
    <el-form-item label="��ѡֵ�б�[�ö��ŷָ�]" prop="valueSelect">
      <el-input v-model="dataForm.valueSelect" placeholder="��ѡֵ�б�[�ö��ŷָ�]"></el-input>
    </el-form-item>
    <el-form-item label="��������[0-�������ԣ�1-�������ԣ�2-���������������ǻ�������]" prop="attrType">
      <el-input v-model="dataForm.attrType" placeholder="��������[0-�������ԣ�1-�������ԣ�2-���������������ǻ�������]"></el-input>
    </el-form-item>
    <el-form-item label="����״̬[0 - ���ã�1 - ����]" prop="enable">
      <el-input v-model="dataForm.enable" placeholder="����״̬[0 - ���ã�1 - ����]"></el-input>
    </el-form-item>
    <el-form-item label="��������" prop="catelogId">
      <el-input v-model="dataForm.catelogId" placeholder="��������"></el-input>
    </el-form-item>
    <el-form-item label="����չʾ���Ƿ�չʾ�ڽ����ϣ�0-�� 1-�ǡ�����sku����Ȼ���Ե���" prop="showDesc">
      <el-input v-model="dataForm.showDesc" placeholder="����չʾ���Ƿ�չʾ�ڽ����ϣ�0-�� 1-�ǡ�����sku����Ȼ���Ե���"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          attrId: 0,
          attrName: '',
          searchType: '',
          icon: '',
          valueSelect: '',
          attrType: '',
          enable: '',
          catelogId: '',
          showDesc: ''
        },
        dataRule: {
          attrName: [
            { required: true, message: '������不能为空', trigger: 'blur' }
          ],
          searchType: [
            { required: true, message: '�Ƿ���Ҫ����[0-����Ҫ��1-��Ҫ]不能为空', trigger: 'blur' }
          ],
          icon: [
            { required: true, message: '����ͼ��不能为空', trigger: 'blur' }
          ],
          valueSelect: [
            { required: true, message: '��ѡֵ�б�[�ö��ŷָ�]不能为空', trigger: 'blur' }
          ],
          attrType: [
            { required: true, message: '��������[0-�������ԣ�1-�������ԣ�2-���������������ǻ�������]不能为空', trigger: 'blur' }
          ],
          enable: [
            { required: true, message: '����״̬[0 - ���ã�1 - ����]不能为空', trigger: 'blur' }
          ],
          catelogId: [
            { required: true, message: '��������不能为空', trigger: 'blur' }
          ],
          showDesc: [
            { required: true, message: '����չʾ���Ƿ�չʾ�ڽ����ϣ�0-�� 1-�ǡ�����sku����Ȼ���Ե���不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.attrId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.attrId) {
            this.$http({
              url: this.$http.adornUrl(`/product/attr/info/${this.dataForm.attrId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.attrName = data.attr.attrName
                this.dataForm.searchType = data.attr.searchType
                this.dataForm.icon = data.attr.icon
                this.dataForm.valueSelect = data.attr.valueSelect
                this.dataForm.attrType = data.attr.attrType
                this.dataForm.enable = data.attr.enable
                this.dataForm.catelogId = data.attr.catelogId
                this.dataForm.showDesc = data.attr.showDesc
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/product/attr/${!this.dataForm.attrId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'attrId': this.dataForm.attrId || undefined,
                'attrName': this.dataForm.attrName,
                'searchType': this.dataForm.searchType,
                'icon': this.dataForm.icon,
                'valueSelect': this.dataForm.valueSelect,
                'attrType': this.dataForm.attrType,
                'enable': this.dataForm.enable,
                'catelogId': this.dataForm.catelogId,
                'showDesc': this.dataForm.showDesc
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
